package com.note.back.controller;

import com.note.back.pojo.Result;
import com.note.back.pojo.SMS;
import com.note.back.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SmsController {

    @Autowired
    private SmsService emailService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("/user/sendCode")
    public Result sendVerificationCode(@RequestBody SMS sms) {
        String email = sms.getEmail();
        try {
            Object value = redisTemplate.opsForValue().get(email);
            if (value != null) {
                return Result.error("验证码已发送，请勿重复点击");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
        try {
            emailService.sendVerificationCode(email);
            return Result.success("验证码发送成功");
        } catch (Exception e) {
            return Result.error("验证码发送失败");
        }
    }
}